<template>
  <div class="Bread">
  <Bread></Bread>
  </div>
  <div class="goodsfenlei">
    <div class="adddata">
      <div class="title">
        <el-icon><Tickets /></el-icon>
        <h3>数据列表</h3>
      </div>
      <div>
        <el-button type="primary" @click="addProductCateFn">添加</el-button>
      </div>
    </div>
    <div class="tables">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="编号" width="100" align="center" />
        <el-table-column prop="name" label="分类名称" width="110" align="center" />
        <el-table-column label="级别" width="100" align="center">
          <template #default="scope">
            <div>
              {{ scope.row.parentId === 0 ? "一级菜单" : "二级菜单" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="productCount"
          label="商品数量"
          width="100"
          align="center"
        />
        <el-table-column prop="productUnit" label="数量单位" width="100" align="center" />
        <el-table-column label="导航栏" width="100" align="center">
          <template #default="scope">
            <div>
              <el-switch
                :value="scope.row.navStatus === 1 ? true : false"
                @click="navStatusFn(scope.row.id, scope.row.navStatus)"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="是否显示" width="100" align="center">
          <template #default="scope">
            <div>
              <el-switch
                :value="scope.row.showStatus === 1 ? true : false"
                @click="ShowStatusFn(scope.row.id, scope.row.showStatus)"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" width="100" align="center" />
        <el-table-column label="设置" align="center">
          <template #default="scope">
            <div>
              <el-button
                @click="lookxiajiFn(scope.row.id)"
                :disabled="scope.row.parentId === 0 ? false : true"
                size="small"
                >查看下级</el-button
              >
              <el-button size="small">转移商品</el-button>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <div>
              <el-button @click="updateProductFn(scope.row.id)" size="small"
                >编辑</el-button
              >
              <el-button type="danger" @click="deltProductFn(scope.row.id)" size="small"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[5, 10, 15, 20]"
          :small="small"
          :disabled="disabled"
          :background="background"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from "vue";
import type { Ref } from "vue";
import * as api from "@/request/api";
import { useRoute, useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import Bread from "@/components/Bread.vue";
//路由跳转
const router = useRouter();
//分页数据
const currentPage = ref(1);
const pageSize = ref(5);
const total = ref(0);
const small = ref(false);
const background = ref(false);
const disabled = ref(false);
//列表数据
let tableData = ref([]) as Ref<any>;
//进入页面发送请求
let getGoodsfenlei = async () => {
  let data = await api.goods.GoodsfenleiList({
    pageNum: currentPage.value,
    pageSize: pageSize.value,
  });
  // console.log(data);
  currentPage.value = data.pageNum;
  pageSize.value = data.pageSize;

  total.value = data.total;
  tableData.value = data.list;
};
getGoodsfenlei();
// console.log(tableData);
//分页发送请求数据
const handleSizeChange = (val: number) => {
  getGoodsfenlei();
  // console.log(`${val} items per page`);
};
const handleCurrentChange = (val: number) => {
  getGoodsfenlei();
  // console.log(`current page: ${val}`);
};
//查看下一级
let lookxiajiFn = (val: number) => {
  // console.log(val);
  router.push({
    path: "/pms/productCate",
    // name: 'home',
    query: {
      parentId: val,
    },
  });
};
//监听路由变化,根据路由参数发送新的请求覆盖数据
watch(
  () => router.currentRoute.value.query,
  (newValue, oldValue) => {
    // console.log(newValue.parentId);
    let getGoodsfenlei = async () => {
      let data = await api.goods.GoodsfenleiList(newValue);
      // console.log(data);
      currentPage.value = data.pageNum;
      pageSize.value = data.pageSize;
      total.value = data.total;
      tableData.value = data.list;
    };
    getGoodsfenlei();
  },
  { immediate: true }
);
//添加逻辑
let addProductCateFn = () => {
  router.push({
    path: "/pms/addProductCate",
  });
};
//编辑数据
let updateProductFn = (val: number) => {
  router.push({
    path: "/pms/updateProductCate",
    // name: 'home',
    query: {
      id: val,
    },
  });
};
//删除逻辑
let deltProductFn = (val: number) => {
  // console.log(row);  // row就是表单数据
  // 二次 确认
  ElMessageBox.confirm("是否删除该分类", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      // console.log(val);

      await api.goods.GoodsDletefenleiList({
        id: val,
      });
      getGoodsfenlei();
      ElMessage({
        type: "success",
        message: "操作成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消提交",
      });
    });
};
//是否显示导航栏模块
let navStatusFn = async (val: number, val2: number) => {
  if (val2 == 1) {
    val2 = 0;
  } else if (val2 == 0) {
    val2 = 1;
  }
  let newData = await api.goods.updatenavStatus({
    ids:val,
    navStatus:val2
  })
  if(newData){
    ElMessage({
      message: "操作成功",
      grouping: true,
      type: "success",
    });
    getGoodsfenlei();
    // console.log(newData);
  }
};
//是否显示
let ShowStatusFn = async (val: number, val2: number) => {
  if (val2 == 1) {
    val2 = 0;
  } else if (val2 == 0) {
    val2 = 1;
  }
  let newData = await api.goods.updatenavShowStatus({
    ids: val,
    showStatus: val2,
  });
  if (newData) {
    ElMessage({
      message: "操作成功",
      grouping: true,
      type: "success",
    });
    getGoodsfenlei();
  }
};
</script>

<style lang="scss" scoped>
.goodsfenlei {
  margin-top: 40px;
  .adddata {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    margin-bottom: 20px;
    border: 1px solid #ebeef5;
    box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
    .title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      h3 {
        color: #303133;
        font-size: 14px;
        margin-left: 10px;
      }
    }
  }
  .pagination {
    margin: 20px 0;
    padding-right: 20px;
    float: right;
  }
}
.Bread {
  position: sticky;
  top: -100px;
  margin: 0;
  width: 100%;
  border-bottom: 1px solid #eee;
  // box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
  padding-left: 20px;
  box-sizing: border-box;
}
</style>
